/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.ast;

import java.io.File;

import alloy.parse.syntaxtree.NodeToken;

/**
 * Represents a continuous extent (range of characters) within a single source file.
 * Immutable.
 */
public final class Location {

    /**
     * File name object to use when input does not come from a file.
     * Assumes no one is insane enough to name a file "[not from file]".
     */
    public final static String NOT_FROM_FILE = "[not from file]";

    /**
     * name of file containing this
     */
    private final String _fileName;


    /**
     * line number in source where this begins
     */
    private final int _beginLine;

    /**
     * line number in source where this ends
     */
    private final int _endLine;

    /**
     * column number in source where this begins
     */
    private final int _beginColumn;

    /**
     * column number in source where this ends
     */
    private final int _endColumn;

    /**
     * unknown or invalid Location
     */
    public static final Location UNKNOWN = new Location(null, -1, -1, -1, -1);

    /**
     * base constructor
     * @param fileName_
     * @param beginLine_
     * @param beginColumn_
     * @param endLine_
     * @param endColumn_
     */
    public Location(String fileName_, int beginLine_, int beginColumn_, int endLine_, int endColumn_) {
        _fileName = fileName_;
        _beginLine = beginLine_;
        _endLine = endLine_;
        _beginColumn = beginColumn_;
        _endColumn = endColumn_;
    }

    // convenience constructors

    public Location(String fileName_, NodeToken token) {
        this(fileName_,
        		token.beginLine,
        		token.beginColumn,
        		token.endLine,
        		token.endColumn);
    }

    public Location(NodeToken token, Node node) {
        this(node.getLocation().getFileName(),
        		token.beginLine,
             token.beginColumn,
             node.getLocation().getEndLine(),
             node.getLocation().getEndColumn());
    }

    public Location(Node node, NodeToken token) {
        this(node.getLocation().getFileName(),
             node.getLocation().getBeginLine(),
             node.getLocation().getBeginColumn(),
             token.endLine, token.endColumn);
    }

	/**
	 * @param node1
	 * @param node2
	 * @constructs a location that begins at the beginning of node1's location,
	 *             ends and the end of node2's location, and uses the filename
	 *             from node1
	 */
    public Location(Node node1, Node node2) {
        this(node1.getLocation().getFileName(),
        		node1.getLocation().getBeginLine(),
        		node1.getLocation().getBeginColumn(),
        		node2.getLocation().getEndLine(),
        		node2.getLocation().getEndColumn());
    }

    public Location(String fileName_, NodeToken token1, NodeToken token2) {
        this(fileName_, token1.beginLine, token1.beginColumn, token2.endLine, token2.endColumn);
    }

    /**
     * get the filename
     */
    public String getFileName() {
        return _fileName;
    }

    /**
     * get the beginning line number
     * @return the line number
     */
    public int getBeginLine() {
    return _beginLine;
    }

    /**
     * get the ending line number
     * @return the line number
     */
    public int getEndLine() {
    return _endLine;
    }

    /**
     * get the beginning column number
     * @return the column number
     */
    public int getBeginColumn() {
    return _beginColumn;
    }

    /**
     * get the ending column number
     * @return the column number
     */
    public int getEndColumn() {
    return _endColumn;
    }

    /*
     * Checks if this Location contains loc
     */
    public boolean subsumes(Location loc)
    {
        boolean subsumes = false;

        //This tests for this case:
        //         this extent:       |----------|
        //         loc extent:          |------|
        //
        if((((_beginLine == loc.getBeginLine()) &&        //loc Begin starts before (or equal) test Begin
             (_beginColumn <= loc.getBeginColumn())) ||
            (_beginLine < loc.getBeginLine()))
           &&
           (((_endLine == loc.getEndLine()) &&            //loc End finishes after (or equal) test End
             (_endColumn >= loc.getEndColumn())) ||
            (_endLine > loc.getEndLine())))
            {
                subsumes = true;
            }

        return subsumes;
    }

    /*
     * Checks if the this Location overlaps loc
     */
    public boolean overlaps(Location loc)
    {
        boolean overlaps = false;
        boolean isSuperSet = false;
        boolean isSubSet = false;
        boolean targetBeginOverlap = false;
        boolean targetEndOverlap = false;

        //This tests for this case:
        //         this extent:       |----------|
        //         loc extent:          |------|
        //
        if(this.subsumes(loc))   //Check if subsumes
            {
                isSuperSet = true;
            }

        //This tests for this case:
        //         this extent:          |------|
        //         loc extent:         |----------|
        //
        else if(loc.subsumes(this))
            {
                isSubSet = true;
            }

        //This tests for this case:
        //         this extent:       |----------|
        //         loc extent:           |----------|
        //
        else if((((_beginLine == loc.getBeginLine()) &&        //this Begin starts before (or equal) loc Begin
                  (_beginColumn <= loc.getBeginColumn())) ||
                 (_beginLine < loc.getBeginLine()))
                &&
                (((_endLine == loc.getBeginLine()) &&          //this End finishes after (or equal) loc Begin
                  (_endColumn >= loc.getBeginColumn())) ||
                 (_endLine > loc.getBeginLine()))
                &&
                (((_endLine == loc.getEndLine()) &&            //this End finishes before (or equal) loc End
                  (_endColumn <= loc.getEndColumn())) ||       //Necessary to test this?
                 (_endLine < loc.getEndLine())))
            {
                //System.out.println("nodeBeginOverlap detected: " + node_.toString());
                targetBeginOverlap = true;
            }

        //This tests for this case:
        //         this extent:       |----------|
        //         loc extent:     |----------|
        //
        else if((((_beginLine == loc.getBeginLine()) &&        //this Begin starts after (or equal) loc Begin
                  (_beginColumn >= loc.getBeginColumn())) ||   //Necessary to test this?
                 (_beginLine > loc.getBeginLine()))
                &&
                (((_beginLine == loc.getEndLine()) &&        //this Begin finishes before (or equal) loc End
                  (_beginColumn <= loc.getEndColumn())) ||
                 (_beginLine < loc.getEndLine()))
                &&
                (((_endLine == loc.getEndLine()) &&            //this End finishes after (or equal) loc End
                  (_endColumn >= loc.getEndColumn())) ||
                 (_endLine > loc.getEndLine())))
            {
                //System.out.println("nodeEndOverlap detected: " + node_.toString());
                targetEndOverlap = true;
            }

        if(isSuperSet || isSubSet || targetBeginOverlap || targetEndOverlap)
            {
                overlaps = true;
            }

        return overlaps;
    }

    public String toString() {
        // columns reported by JavaCC are always one too high
        int beginColumn = _beginColumn - 1;
        int endColumn = _endColumn - 1;
        return (_fileName == null)
               ? "unknown"
               : (new File(_fileName)).getName() + " " + _beginLine + ":" + beginColumn + "-" + _endLine + ":" + endColumn;
    }
}
